package top.lisicheng.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lisicheng.model.CommonResult;
import top.lisicheng.openfeign.PaymentApi;

import javax.annotation.Resource;

/**
 * @author Lisicheng
 */
@RestController
@Slf4j
@RequestMapping("order")
// 配置默认降价方法
@DefaultProperties(defaultFallback = "timeHandler")
public class OrderController {

    @Autowired
    private PaymentApi paymentApi;

    @GetMapping("get")
    public CommonResult get(Long id) {
        return paymentApi.get(id);
    }

    @RequestMapping("timeout")
    // @HystrixCommand(fallbackMethod = "timeHandler", commandProperties = {
    //         @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    // })
    // 配置了默认的降级方法后，开启hystrix后未指定超时时间，则默认为1秒
    @HystrixCommand
    public CommonResult timeout() {
       int a = 10/0;
        return paymentApi.timeout();
    }

    public CommonResult timeHandler() {

        return new CommonResult(200, "order 80 请求失败，稍等");
    }
}
